package br.usp.icmc.biomac.client.view.impl;

import java.util.ArrayList;
import java.util.List;

import br.usp.icmc.biomac.client.Resources.GlobalResources;
import br.usp.icmc.biomac.client.activity.ImageListActivity;
import br.usp.icmc.biomac.client.view.DicomResultCellBrowser;
import br.usp.icmc.biomac.client.view.DicomResultCellTable;
import br.usp.icmc.biomac.client.view.DicomSearchWidget;
import br.usp.icmc.biomac.client.view.ImageListView;
import br.usp.icmc.biomac.client.view.ImageView;
import br.usp.icmc.biomac.shared.CSVData;
import br.usp.icmc.biomac.shared.CSVSerie;
import br.usp.icmc.biomac.shared.ImageDicom;
import br.usp.icmc.dilvan.aimTemplateViewer.client.aimTemplate.TemplateAIMTemplate;
import br.usp.icmc.dilvan.aimTemplateViewer.client.aimTemplate.TemplateContainerAIMTemplate;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;

public class ImageListViewImpl extends Composite implements ImageListView {

	public ImageListActivity imageListActivity;
	
	private static final Binder binder = GWT.create(Binder.class);
	
	//@UiField  public ImageViewImpl imageViewImpl = new ImageViewImpl();
	
	@UiField SimplePanel dicomSearchWidgetPanel;
	@UiField SimplePanel imageViewPanel;
	@UiField ScrollPanel tablePanel;
	@UiField FlowPanel panelCanvas;
	
	boolean load = false;	
	
	private DicomResultCellTable dicomResultCellTable;
	private DicomResultCellBrowser dicomResultCellBrowser;
	private DicomSearchWidget dicomSearchWidget;
	private ImageView imageView;
	
	public static final GlobalResources RESOURCE = GWT
			.create(GlobalResources.class);


	interface Binder extends UiBinder<Widget, ImageListViewImpl> {
	}

	public ImageListViewImpl() {
		initWidget(binder.createAndBindUi(this));
	}

	public ImageListViewImpl(DicomSearchWidget dicomSearchWidget,DicomResultCellTable dicomResultCellTable, DicomResultCellBrowser dicomResultCellBrowser,ImageView imageView) {
		this.dicomSearchWidget = dicomSearchWidget;
		this.imageView = imageView;
		
		initWidget(binder.createAndBindUi(this));
		
		this.dicomResultCellTable = dicomResultCellTable;
		this.dicomResultCellBrowser = dicomResultCellBrowser;
		this.dicomSearchWidgetPanel.add(this.dicomSearchWidget);
		this.imageViewPanel.add(this.imageView);
		this.tablePanel.add(dicomResultCellTable);
			
	}
	
	@Override
	public void initTableCellResult(List<CSVData> csvData) {
		dicomResultCellTable.setDataResult(csvData);
	}
	
	@Override
	public void initBrowserCellResult(List<CSVData> csvData) {
		dicomResultCellBrowser.setBrowserResult(csvData);
	}
	
	@Override
	public void startDicomView(){
		if (load)
			return;
		List<ImageDicom> Im = new ArrayList<ImageDicom>();
		/*
		Im.add(new ImageDicom("http://biomac.icmc.usp.br:8888/wado?requestType=WADO&studyUID=1.2.752.24.7.19011385.514521&seriesUID=1.2.840.113704.1.111.1804.1218052058.19&objectUID=1.2.840.113704.1.111.3788.1218052169.1545","0"));
		Im.add(new ImageDicom("http://biomac.icmc.usp.br:8888/wado?requestType=WADO&studyUID=1.2.752.24.7.19011385.514521&seriesUID=1.2.840.113704.1.111.1804.1218052058.19&objectUID=1.2.840.113704.1.111.3788.1218052169.1546","1"));
		Im.add(new ImageDicom("http://biomac.icmc.usp.br:8888/wado?requestType=WADO&studyUID=1.2.752.24.7.19011385.514521&seriesUID=1.2.840.113704.1.111.1804.1218052058.19&objectUID=1.2.840.113704.1.111.3788.1218052171.1547","2"));
		*/
		for(int i = 0; i < 10; i++) {//max 86
			String tmp = Integer.toString(i+1);
			if(i<9)
				tmp = "0"+tmp;
			Im.add(new ImageDicom("http://biomac.icmc.usp.br/dicom/7/I03_00"+tmp,Integer.toString(i)));
		}
		imageListActivity.getImages();
		setImages(imageListActivity.getImages());
		load = true;
	}

	@Override
	public Widget asWidget() {
		return this;
	}
	
	@Override
	public void loadThubmanais(final List<CSVSerie> img) {
		GWT.log("loadThubmanais");
		panelCanvas.clear();
		for (int i = 1; i <= img.size(); i++) {
			String url = img.get(i).getThumbnailURL().trim();
			final Image image = new Image(url);
			image.setVisible(false);
			ThumbnailCellImpl thumbnail = new ThumbnailCellImpl(url, img.get(i).getExamType(), img.get(i).getImagesInSeries());
			thumbnail.setPresenter(imageListActivity);
			panelCanvas.add(thumbnail);
		}
	}
	
	


	@Override
	public void setImages(List<ImageDicom> Images) {
		imageView.setData(Images);

	}

	@Override
	public void setPresenter(ImageListActivity imageListActivity) {
		this.imageListActivity = imageListActivity;
		dicomSearchWidget.setPresenter(imageListActivity);
		dicomResultCellTable.setPresenter(imageListActivity);
		dicomResultCellBrowser.setPresenter(imageListActivity);
		imageView.setPresenter(imageListActivity);
	}
	
	@Override
	public ImageView getImageView() {
		return imageView;
	}
	
	// methods for templates selections and components generate the form annotation.
	
	@Override
	public void loadTemplateContainer(List<TemplateContainerAIMTemplate> container) {
		//imageView.getImageAnnotation().setTemplatesContainers(container);
	}
	
	@Override
	public void loadTemplates(List<TemplateAIMTemplate> template) {
		//imageView.getImageAnnotation().setTemplates(template);
	}
	
	@Override
	public void loadTemplate(TemplateAIMTemplate template) {
		//imageView.getImageAnnotation().createForm(template.getComponents());
	}

	@Override
	public void setDicomSearchWidget(DicomSearchWidget dicomSearchWidget) {
		this.dicomSearchWidget = dicomSearchWidget;
	}

	@Override
	public void setDicomResultCellTable(DicomResultCellTable cellResultCellTable) {
		this.dicomResultCellTable = cellResultCellTable;
	}
	@Override
	public void setDicomResultCellBrowser(DicomResultCellBrowser cellResultCellBrowser) {
		this.dicomResultCellBrowser = cellResultCellBrowser;
	}

	@Override
	public void setImageView(ImageView imageView) {
		this.imageView = imageView;
	}
}
